{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "import numpy as np\n",
    "\n",
    "import pickle as pkl\n",
    "import pandas as pd\n",
    "import random\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "import datetime\n",
    "import math\n",
    "import numpy as np\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import Module, Parameter\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./Data/Processed/word_emb_tensor.pkl', 'rb') as f:\n",
    "    word_embed_tensor = pkl.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 105542),\n",
       " array([[1.0000004 , 0.96316344, 0.94975275, ..., 0.93497366, 0.87419015,\n",
       "         0.9190496 ]], dtype=float32),\n",
       " torch.Size([1, 100]),\n",
       " tensor([[     0,  71173,  72495, 101344,  30902,  10109,  94767,  63573,  40478,\n",
       "           96804,   1698,  80935,  36088,  35623,  47253,  47254,  62638,  72486,\n",
       "           64657,  15147,  15137,  87495,  77177, 102303,  62641,  89179, 103220,\n",
       "           32316,  76473,  88971,  83527,  60829,  91226,  62634,  86782,  77092,\n",
       "           87486,   5317,  90082,  69638,  71518,  95402,  83574,  87546,  58658,\n",
       "           10944,  44866,  99615,  94970,  90198, 101064,  24207,   3505,  44962,\n",
       "            3510,  71011,  99189,  78285,  72597,   8803,   8544, 102963,  32656,\n",
       "           60349, 105099,  72497,  23874,    115,  71962,  87493,  19701,  17617,\n",
       "           39395, 104309,  95000,  65505,  14904,  45920,  42394,  76305,  14066,\n",
       "           57269,  71903,  14076,  30921,  14569,  14575,  14132,   6144,   4215,\n",
       "           40406,  98446,  44871, 103616,  14062,  26183,  76834,  29007,    290,\n",
       "           12789]]))"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cos_sim = cosine_similarity(word_embed_tensor[0:1], word_embed_tensor)\n",
    "ind = torch.topk(torch.tensor(cos_sim), 100)[1]\n",
    "cos_sim.shape, cos_sim, ind.shape, ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_60202/1824144176.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword_embed_tensor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m     \u001b[0mcos_sim\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcosine_similarity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword_embed_tensor\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mword_embed_tensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m     \u001b[0mind\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtopk\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcos_sim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m     \u001b[0mtopk_index\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mind\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/SR-GNN/lib/python3.7/site-packages/sklearn/metrics/pairwise.py\u001b[0m in \u001b[0;36mcosine_similarity\u001b[0;34m(X, Y, dense_output)\u001b[0m\n\u001b[1;32m   1250\u001b[0m         \u001b[0mY_normalized\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mX_normalized\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1251\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1252\u001b[0;31m         \u001b[0mY_normalized\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnormalize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mY\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1253\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1254\u001b[0m     \u001b[0mK\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msafe_sparse_dot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX_normalized\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY_normalized\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdense_output\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdense_output\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/SR-GNN/lib/python3.7/site-packages/sklearn/preprocessing/_data.py\u001b[0m in \u001b[0;36mnormalize\u001b[0;34m(X, norm, axis, copy, return_norm)\u001b[0m\n\u001b[1;32m   1825\u001b[0m             \u001b[0mnorms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1826\u001b[0m         \u001b[0mnorms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_handle_zeros_in_scale\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnorms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1827\u001b[0;31m         \u001b[0mX\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0mnorms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1828\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1829\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "topk_index = []\n",
    "for i in range(word_embed_tensor.shape[0]):\n",
    "    if i%1000==0: print(i)\n",
    "    cos_sim = cosine_similarity(word_embed_tensor[i:i+1], word_embed_tensor)\n",
    "    ind = torch.topk(torch.tensor(cos_sim), 100)[1]\n",
    "    topk_index.append(ind)\n",
    "topk_index = np.concatenate(topk_index)\n",
    "topk_index.shape, topk_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[     0,  71173,  72495, 101344,  30902,  10109,  94767,  63573,\n",
       "         40478,  96804,   1698,  80935,  36088,  35623,  47253,  47254,\n",
       "         62638,  72486,  64657,  15147,  15137,  87495,  77177, 102303,\n",
       "         62641,  89179, 103220,  32316,  76473,  88971,  83527,  60829,\n",
       "         91226,  62634,  86782,  77092,  87486,   5317,  90082,  69638,\n",
       "         71518,  95402,  83574,  87546,  58658,  10944,  44866,  99615,\n",
       "         94970,  90198, 101064,  24207,   3505,  44962,   3510,  71011,\n",
       "         99189,  78285,  72597,   8803,   8544, 102963,  32656,  60349,\n",
       "        105099,  72497,  23874,    115,  71962,  87493,  19701,  17617,\n",
       "         39395, 104309,  95000,  65505,  14904,  45920,  42394,  76305,\n",
       "         14066,  57269,  71903,  14076,  30921,  14569,  14575,  14132,\n",
       "          6144,   4215,  40406,  98446,  44871, 103616,  14062,  26183,\n",
       "         76834,  29007,    290,  12789],\n",
       "       [     0,  71173,  72495, 101344,  30902,  10109,  94767,  63573,\n",
       "         40478,  96804,   1698,  80935,  36088,  35623,  47253,  47254,\n",
       "         62638,  72486,  64657,  15147,  15137,  87495,  77177, 102303,\n",
       "         62641,  89179, 103220,  32316,  76473,  88971,  83527,  60829,\n",
       "         91226,  62634,  86782,  77092,  87486,   5317,  90082,  69638,\n",
       "         71518,  95402,  83574,  87546,  58658,  10944,  44866,  99615,\n",
       "         94970,  90198, 101064,  24207,   3505,  44962,   3510,  71011,\n",
       "         99189,  78285,  72597,   8803,   8544, 102963,  32656,  60349,\n",
       "        105099,  72497,  23874,    115,  71962,  87493,  19701,  17617,\n",
       "         39395, 104309,  95000,  65505,  14904,  45920,  42394,  76305,\n",
       "         14066,  57269,  71903,  14076,  30921,  14569,  14575,  14132,\n",
       "          6144,   4215,  40406,  98446,  44871, 103616,  14062,  26183,\n",
       "         76834,  29007,    290,  12789]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([ind, ind])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7c2e65e64076883662e9fbb467097aa6d81839e6b77012bdd0b6d8c4fbfb9623"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 ('SR-GNN')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
